package com.example.bysj.Controller;
//201902104061徐中堂
import com.example.bysj.Service.GraduateProjectService;
import com.example.bysj.domain.GraduateProject;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
//父链接，RESTful风格采用同一个URI，通过不同的HTTP方法来实现对该资源不同的操作
@RequestMapping("/graduateProjectInfoSys")
//返回json格式
@RestController
public class GraduateProjectController {
    //自动注入
    @Autowired
    private GraduateProjectService graduateProjectService;
    //添加
    @PostMapping("/graduateProject")
    //参数为一个对象
    public String add(@RequestBody GraduateProject graduateProject ) {
        boolean added =graduateProjectService.add(graduateProject);
        if (added){
            log.info("添加成功！");
            return "添加成功！";
        }else {
            log.info("添加失败！");
            return "添加失败！";
        }
    }
    //查找全部
    @GetMapping("/graduateProject")
    public List<GraduateProject> findALL(){
        return graduateProjectService.findAll();
    }
    //根据id查找
    @GetMapping("/graduateProject/{id}")
    public GraduateProject find(@PathVariable("id") int id){
        GraduateProject graduateProject = graduateProjectService.find(id);
        if(graduateProject!=null){
            return graduateProject;
        }else {
            //自定义一个运行时异常
            throw new RuntimeException("未查询到 "+id+" !");
        }
    }
    //修改
    @PutMapping("/graduateProject/{id}")
    //参数为修改后对象和被修改对象的id
    public String update(@PathVariable(value = "id") int id,
                         @RequestBody GraduateProject graduateProject){
        //将参数id赋值给对象
        graduateProject.setId(id);
        boolean updated = graduateProjectService.update(graduateProject);
        if(updated){
            return "修改成功！ ";
        }else {
            return "修改失败！ ";
        }
    }
    //删除
    @DeleteMapping("/graduateProject/{id}")
    public String delete(@PathVariable("id") int id){
        boolean deleted=graduateProjectService.delete(id);
        if (deleted){
            return "删除成功！ ";
        }else {
            return "删除失败！ ";
        }
    }
}
